package com.weng.platform.platform.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.platform.entity.ProjectUserRel;
import com.weng.platform.platform.model.vo.ProjectUserRelVo;
import com.weng.platform.platform.service.IProjectUserRelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;


/**
 * 项目和用户关联 API实现
 *
 * @author weng
 * @date 2021-01-15 17:18:45
 *
 */  
@Slf4j
@RestController
@RequestMapping("/project-user-rels")
@Api(value = "项目和用户关联api", tags = {"项目和用户关联操作接口"})
public class ProjectUserRelApi extends BaseController {
    @Autowired
    private IProjectUserRelService projectUserRelService;

    /**
     * 功能描述:单个获取项目和用户关联
     */
    @ApiOperation(value = "获取项目和用户关联", notes = "")
    @GetMapping(value = "/{id}")
    public ResponseData get(@PathVariable("id") String id){
        ProjectUserRel projectUserRel = projectUserRelService.getById(id);
        return success(projectUserRel);
    }


    /**
     * 条件 分页查询项目和用户关联
     */
    @ApiOperation(value = "分页获取项目和用户关联", notes = "")
    @GetMapping(value = "")
    public ResponseData list(Integer pageNo,Integer pageSize){
		IPage<ProjectUserRel> iPage = projectUserRelService.list(pageNo, pageSize);
        return success(iPage);
    }


    /**
	 * 新增项目和用户关联
	 */
	@ApiOperation(value = "新增项目和用户关联", notes = "")
	@PostMapping(value = "")
	public ResponseData add(@Valid @RequestBody ProjectUserRelVo projectUserRelReq){
		ProjectUserRel projectUserRel = Convert.convert(ProjectUserRel.class, projectUserRelReq);
		projectUserRelService.add(projectUserRel);
		return success();
	}

	/**
	 * 更新项目和用户关联
	 */
	@ApiOperation(value = "更新项目和用户关联", notes = "")
	@PutMapping(value = "")
	public ResponseData update(@Valid @RequestBody ProjectUserRelVo projectUserRelReq){
		ProjectUserRel projectUserRel = Convert.convert(ProjectUserRel.class, projectUserRelReq);
		projectUserRelService.update(projectUserRel);
		return success();
	}


	/**
	 * 删除项目和用户关联
	 */
	@ApiOperation(value = "ids 删除项目和用户关联", notes = "可以删除多个，中间以,号分隔")
	@DeleteMapping(value = "")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,id不能为空！");
		}
		projectUserRelService.removeByIds(Arrays.asList(ids.split(",")));
		return success(deleteReq.getIds());
	}
}
